package o;

import android.content.Context;
import android.media.MediaScannerConnection;
import android.os.AsyncTask;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class blt extends AsyncTask<Void, Void, Boolean> {
    private String ˊ;
    private b ˋ;
    private Context ˎ;
    private String ॱ;

    public interface b {
        void ˏ(blt o_blt, String str, boolean z);
    }

    protected /* synthetic */ Object doInBackground(Object[] objArr) {
        return ˋ((Void[]) objArr);
    }

    protected /* synthetic */ void onPostExecute(Object obj) {
        ˋ((Boolean) obj);
    }

    public void ˏ(b bVar) {
        this.ˋ = bVar;
    }

    public blt(Context context, String str, String str2) {
        this.ˎ = context;
        this.ॱ = str;
        this.ˊ = str2;
    }

    protected Boolean ˋ(Void... voidArr) {
        String ॱ = ॱ(this.ॱ);
        File file = new File(this.ˊ);
        if (file.exists() || file.mkdirs()) {
            file = cea.ˋ(this.ˎ, this.ॱ);
            if (ॱ.equals("")) {
                if (file != null) {
                    ॱ = ॱ(file.getPath());
                } else {
                    blf.ˋ.ˊ("DownloadPicTask", "picture file is null");
                    return Boolean.valueOf(false);
                }
            }
            if (!ˏ(file, new File(this.ˊ, ॱ))) {
                return Boolean.valueOf(false);
            }
            MediaScannerConnection.scanFile(this.ˎ, new String[]{this.ˊ + "/" + ॱ}, null, null);
            return Boolean.valueOf(true);
        }
        blf.ˋ.ˊ("DownloadPicTask", "create saveImg dir failed.");
        return Boolean.valueOf(false);
    }

    private boolean ˏ(File file, File file2) {
        Closeable fileOutputStream;
        Exception e;
        Throwable th;
        Closeable closeable = null;
        if (file == null || file2 == null) {
            return false;
        }
        try {
            Closeable fileInputStream = new FileInputStream(file);
            try {
                fileOutputStream = new FileOutputStream(file2);
            } catch (Exception e2) {
                e = e2;
                fileOutputStream = null;
                closeable = fileInputStream;
                try {
                    blf.ˋ.ˏ("DownloadPicTask", "DownloadPicTask copyToFile fail" + e.toString());
                    dnw.ˎ(closeable);
                    dnw.ˎ(fileOutputStream);
                    return false;
                } catch (Throwable th2) {
                    th = th2;
                    dnw.ˎ(closeable);
                    dnw.ˎ(fileOutputStream);
                    throw th;
                }
            } catch (Throwable th3) {
                th = th3;
                fileOutputStream = null;
                closeable = fileInputStream;
                dnw.ˎ(closeable);
                dnw.ˎ(fileOutputStream);
                throw th;
            }
            try {
                byte[] bArr = new byte[8192];
                while (true) {
                    int read = fileInputStream.read(bArr);
                    if (read != -1) {
                        fileOutputStream.write(bArr, 0, read);
                    } else {
                        fileOutputStream.flush();
                        dnw.ˎ(fileInputStream);
                        dnw.ˎ(fileOutputStream);
                        return true;
                    }
                }
            } catch (Exception e3) {
                e = e3;
                closeable = fileInputStream;
                blf.ˋ.ˏ("DownloadPicTask", "DownloadPicTask copyToFile fail" + e.toString());
                dnw.ˎ(closeable);
                dnw.ˎ(fileOutputStream);
                return false;
            } catch (Throwable th4) {
                th = th4;
                closeable = fileInputStream;
                dnw.ˎ(closeable);
                dnw.ˎ(fileOutputStream);
                throw th;
            }
        } catch (Exception e4) {
            e = e4;
            fileOutputStream = null;
            blf.ˋ.ˏ("DownloadPicTask", "DownloadPicTask copyToFile fail" + e.toString());
            dnw.ˎ(closeable);
            dnw.ˎ(fileOutputStream);
            return false;
        } catch (Throwable th5) {
            th = th5;
            fileOutputStream = null;
            dnw.ˎ(closeable);
            dnw.ˎ(fileOutputStream);
            throw th;
        }
    }

    protected void ˋ(Boolean bool) {
        if (this.ˋ != null) {
            this.ˋ.ˏ(this, this.ॱ, bool.booleanValue());
        }
    }

    private String ॱ(String str) {
        String str2 = "";
        if (str != null) {
            try {
                if (str.contains("/")) {
                    str2 = str.substring(str.lastIndexOf("/") + 1, str.length());
                }
            } catch (Exception e) {
                blf.ˋ.ˏ("DownloadPicTask", "getFileName Exception = " + e.toString());
            }
        }
        return str2;
    }
}
